package org.tong.order.jpa.daos;

import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.stereotype.Component;
import org.tong.order.jpa.OrderTccDaoI;

/**
 * @author
 * @date 2022-12-22 9:31
 */
@Component
public class OrderTccDao implements OrderTccDaoI {
    @Override
    public boolean prepare(BusinessActionContext businessActionContext, Long id, Long amount) {
        String xid = businessActionContext.getXid();
        long branchId = businessActionContext.getBranchId();
        System.out.println(String.format("OrderTccDao prepare, xid:%s, branchId:%d", xid, branchId));
        System.out.println(String.format("OrderTccDao prepare, id:%d, amount:%d", id, amount));
        return true;
    }

    @Override
    public boolean commit(BusinessActionContext businessActionContext) {
        String xid = businessActionContext.getXid();
        long branchId = businessActionContext.getBranchId();
        System.out.println(String.format("OrderTccDao commit, xid:%s, branchId:%d", xid, branchId));

        Long id = Long.valueOf(businessActionContext.getActionContext("id").toString());
        Long amount = Long.valueOf(businessActionContext.getActionContext("amount").toString());
        System.out.println(String.format("OrderTccDao commit, id:%d, amount:%d", id, amount));
        return true;
    }

    @Override
    public boolean cancel(BusinessActionContext businessActionContext) {
        String xid = businessActionContext.getXid();
        long branchId = businessActionContext.getBranchId();
        System.out.println(String.format("OrderTccDao cancel, xid:%s, branchId:%d", xid, branchId));
        Long id = Long.valueOf(businessActionContext.getActionContext("id").toString());
        Long amount = Long.valueOf(businessActionContext.getActionContext("amount").toString());
        System.out.println(String.format("OrderTccDao cancel, id:%d, amount:%d", id, amount));
        return true;
    }
}
